<template>
  <div class="casecause-page">
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="dataTable"
      :options="options"
      @row-click="rowClick"
      selection-row
      @selection-change="selectChange"
      @current-change="handleCurrentChange"
      :pagination="pagination"
      @pagination-current-change="paginationCurrentChange"
    >
      <div class="title" slot="header">
        <span class="text">违法行为列表</span>
        <head-btns @add="addDept" @mod="editDept" @del="delDept" @bind="bind"/>
      </div>
      <!-- <el-button slot="header" type="primary" style="margin-bottom: 5px" @click="addDept" icon="el-icon-circle-plus">添加</el-button>
            <el-button slot="header" type="success" style="margin-bottom: 5px" @click="editDept" icon="el-icon-edit">修改</el-button>
      <el-button slot="header" type="danger" style="margin-bottom: 5px" @click="delDept" icon="el-icon-delete">删除</el-button>-->
    </d2-crud>
    <edit-form :dept="dept" v-model="editFormVisible" @submit="fetchDataList"/>
    <add-form v-model="addFormVisible" @submit="fetchDataList"></add-form>
    <bind-form :bindModel="bindModel" v-model="bindFormVisible" @submit="fetchDataList"/>
  </div>
</template>
<script>
  import dictCache from '@/assets/utils/cache-dict-util';
  import casecauseApi from "@/api/system/casecause";
  import editForm from "./editForm";
  import addForm from "./addForm";
  import bindForm from "./bindForm";
  import HeadBtns from "@/views/comps/zhzf/head-btns";
  import utils from "@/assets/utils/zhzf-util";
  const causeType = '0055';
  export default {
    name: "casecausePage",
    components: {editForm, addForm,bindForm, HeadBtns},
    data() {
      return {
        columns: [
          {
          title: "序号",
          key: "no",
          width: 80,
          formatter: (row, col, val, index) => {
            
            let flag =
              (this.pagination.pageIndex - 1) * this.pagination.pageSize;
              
            return flag + index + 1;
          }
        },
          {
            title: "执法门类",
            key: "zfmlId",
            width: "100",
            formatter: (row, col, val) => {
              // let info = await dictCache.getDataInfo('0055', val);
              // console.log(info.name)
              return dictCache.getDataInfo(causeType, val).name
            }
          },
          {
            title: "违法行为编号",
            key: "casecauseNumber",
            width: "120"
          },
          {
            title: "内容",
            key: "content"
          }
        ],
        data: [],
        dataTable: [],
        options: {
          stripe: true,
          highlightCurrentRow: true
        },
        currentId: null,
        editFormVisible: false,
        dept: {},
        bindModel: {},
        addFormVisible: false,
        bindFormVisible : false,
        pagination: {
          currentPage: 1,
          pageSize: utils.getPageSize(),
          total: 0
        },
        selected: []
      };
    },
    mounted() {
      this.initPage();
    },
    methods: {
      async initPage() {
        await dictCache.getDataList(causeType);
        this.fetchDataList();
      },
      async fetchDataList() {
        let res = await casecauseApi.Page(this.pagination);
        var data = res.data;
        this.data = data.rows;
        this.dataTable = data.rows;
        this.pagination.total = data.total;
      },
      paginationCurrentChange(currentPage) {
        this.pagination.currentPage = currentPage;
        this.fetchDataList();
      },
      rowClick(row) {
        this.$refs.d2Crud.$children[1].toggleRowSelection(row);
      },
      selectChange(selection) {
        this.selected = selection;
      },
      addDept() {
        this.addFormVisible = true;
      },
      bind() {
        if (this.currentId) {
          this.bindModel.causeId = this.currentId;
          this.bindFormVisible = true;
        }
      },
      handleCurrentChange(currentRow) {
        if (currentRow) {
          this.currentId = currentRow.casecauseId;
        }
      },
      editDept() {
        if (this.currentId) {
          for (let i = 0; i < this.data.length; i++) {
            if (this.data[i].casecauseId == this.currentId) {
              this.dept = this.data[i];
            }
          }
          this.editFormVisible = true;
        }
      },
      delDept() {
        this.$confirm("确认删除？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        }).then(async () => {
          await casecauseApi.del({id: this.currentId});
          this.fetchDataList();
          this.currentId = null;
        });
      }
    }
  };
</script>

<style lang="scss">
  .casecause-page {
    .d2-crud {
      .d2-crud-body {
        height: calc(100% - 75px) !important;
      }
    }
  }
</style>
